{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Recursion\n", "\n", "Defining something in terms of itself.\n", "\n", "\"You can't do that!\" - Mom\n", "\n", "Yes, you can.\n", "\n", "## Factorial\n", "\n", "factorial of $5 = 5 * 4 * 3 * 2 * 1$\n", "\n", "* The factorial of 1 is 1.\n", "* The factorial of n is the factorial(n - 1) * n." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_10\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_10\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_10\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_10\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #10:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #10 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "factorial(1): 1\n", "factorial(2): 2\n", "factorial(3): 6\n", "factorial(4): 24\n", "factorial(5): 120\n", "factorial(6): 720\n", "factorial(7): 5040\n", "factorial(8): 40320\n", "factorial(9): 362880\n", "factorial(10): 3628800\n" ] } ], "source": [ "int factorial(int n) {\n", " if (n == 1)\n", " return 1;\n", " else\n", " return factorial(n - 1) * n;\n", "}\n", "\n", "void setup() {\n", " for (int i = 1; i < 11; i++) {\n", " println(\"factorial(\" + i + \"): \" + factorial(i));\n", " }\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fibonacci\n", "\n", "Fibonacci sequence: 1 1 2 3 5 8 13\n", "\n", "* The fib of 1 is 1.\n", "* The fib of 2 is 1.\n", "* The fib of n is the fib(n - 1) + fib(n - 2)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_6\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_6\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_6\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_6\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #6:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #6 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "fib(3): 2\n", "fib(4): 3\n", "fib(5): 5\n", "fib(6): 8\n", "fib(3): 2\n", "fib(4): 3\n", "fib(5): 5\n", "fib(6): 8\n" ] } ], "source": [ "int fib(int n) {\n", " if (n == 1) {\n", " return 1;\n", " } else if (n == 2) {\n", " return 1;\n", " } else {\n", " return fib(n - 1) + fib(n - 2);\n", " }\n", "}\n", "\n", "void setup() {\n", " println(\"fib(3): \" + fib(3));\n", " println(\"fib(4): \" + fib(4));\n", " println(\"fib(5): \" + fib(5));\n", " println(\"fib(6): \" + fib(6));\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }